<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <el-select v-model="query.state" clearable size="small" placeholder="状态" class="filter-item" style="width: 190px" @change="crud.toQuery">
          <el-option v-for="item in dict.read_status" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </div>

      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @row-click="messageRowClick" @selection-change="crud.selectionChangeHandler">
        <el-table-column prop="title" label="标题" width="235">
          <template slot-scope="scope">
            {{ scope.row.notifyAnnounce.title }}
          </template>
        </el-table-column>
        <el-table-column prop="message" label="内容">
          <template slot-scope="scope">
            {{ scope.row.notifyAnnounce.message }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="155" />
        <el-table-column prop="state" label="已读状态" width="85">
          <template slot-scope="scope">
            <el-tag :type="scope.row.state ? 'info' : 'danger'">
              {{ dict.label.read_status[`${scope.row.state}`] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="85">
          <template slot-scope="scope">
            <el-button v-if="!scope.row.state" size="mini" type="text" @click="changeState(scope.row)">
              已阅
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudNotifyAnnounceUser from '@/api/message/notifyAnnounceUser'
import CRUD, { presenter, header, crud } from '@crud/crud'
import pagination from '@crud/Pagination'

export default {
  name: 'NotifyAnnounceUser',
  components: { pagination },
  mixins: [presenter(), header(), crud()],
  dicts: ['read_status'],
  cruds() {
    return CRUD({ title: '用户消息', url: 'api/notifyAnnounceUser', idField: 'id', sort: 'id,desc', crudMethod: { ...crudNotifyAnnounceUser }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'notifyAnnounceUser:add'],
        edit: ['admin', 'notifyAnnounceUser:edit'],
        del: ['admin', 'notifyAnnounceUser:del']
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    changeState(row) {
      row.state = true
      crudNotifyAnnounceUser.edit(row).then(res => {
        this.crud.toQuery()
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },

    messageRowClick(row) {
      if (row.notifyAnnounce.router) {
        this.$router.push(row.notifyAnnounce.router)
      }
      // 如果是未讀，需要更改狀態
      if (!row.state) {
        this.changeState(row)
      }
    }
  }
}
</script>

<style scoped>

</style>
